package com.example.hotel.controller;

import com.example.hotel.common.JsonResult;
import com.example.hotel.entity.Equip;
import com.example.hotel.service.EquipService;
import com.example.hotel.vo.EquipVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "客房物品管理")
@RestController
@CrossOrigin
@RequestMapping("equip")
public class EquipController {

    @Autowired
    EquipService service;

    @ApiOperation(value = "获取所有客房物品信息")
    @GetMapping("findAll")
    public JsonResult<List<EquipVO>> findAll() {
        List<EquipVO> list = service.findAll();
        if (!(list.isEmpty())) {
            return JsonResult.ok(list);
        } else {
            return JsonResult.fail(5001, "所有客房物品查询失败");
        }
    }

    @ApiOperation(value = "添加客房物品")
    @GetMapping("insert")
    public JsonResult insert(Equip equip) {
        try {
            int r = service.insert(equip);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "客房物品添加失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }
    }

    @ApiOperation(value = "删除客房物品信息")
    @DeleteMapping("delete")
    public JsonResult deleteById(Integer equip_id) {
        try {
            if (equip_id == null) {
                return JsonResult.fail(5001, "客房物品id不能为空");
            }
            int r = service.deleteById(equip_id);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "客房物品删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }
    }

    @ApiOperation(value = "更新客房物品信息")
    @GetMapping("update")
    public JsonResult updateEquip(@Param("equipId") Integer equipId, @Param("roomId") Integer roomId, @Param("itemId") Integer itemId, @Param("state") String state, @Param("num") Integer num) {
        try {
            Equip equip = new Equip();
            equip.setEquipId(equipId);
            equip.setRoomId(roomId);
            equip.setItemId(itemId);
            equip.setNum(num);
            equip.setState(state);

            int r = service.update(equip);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "客房物品更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }


    }
}


